package tech.aistar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import tech.aistar.mapper.CartMapper;
import tech.aistar.mapper.CollectMapper;
import tech.aistar.model.entity.Cart;
import tech.aistar.model.entity.Collect;
import tech.aistar.service.ICollectService;
import tech.aistar.util.MyBatisUtil;

import java.util.List;

public class CollectServiceImpl implements ICollectService {
    @Override
    public void addCollect(Collect collect) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        CollectMapper collectMapper= sqlSession.getMapper(CollectMapper.class);
//        Cart c = cartMapper.find(cart.getProductId(),cart.getUserId());
          Collect collect1 = collectMapper.find(collect.getProductId(),collect.getUserId());
           if(collect1==null)
           {
               collectMapper.save(collect);

           }
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
    }

    @Override
    public int delById(Integer id) {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        CollectMapper collectMapper = sqlSession.getMapper(CollectMapper.class);
        int x = collectMapper.delById(id);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return  x ;


    }

    @Override
    public PageInfo<Collect> findAll(Integer userId, Integer pageNow, Integer pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        CollectMapper collectMapper = sqlSession.getMapper(CollectMapper.class);
        PageHelper.startPage(pageNow,pageSize);
        List<Collect> collectList =collectMapper.findAll(userId);

        PageInfo<Collect> pageInfo = new PageInfo<>(collectList);
        MyBatisUtil.closeSqlSession(sqlSession);
        return pageInfo;
    }
}
